
    /**  
    * @Title: MailSenderFactory.java
    * @Package com.efast.cafe.util.mail
    * @Description: TODO(用一句话描述该文件做什么)
    * @author Administrator
    * @date 2018年11月20日
    * @version V1.0  
    */
    
package com.efast.cafe.framework.mail;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.template.Template;

/**
    * @ClassName: MailSenderService
    * @Description:邮件发送
    * @author Administrator
    * @date 2018年11月20日
    *
    */
@Service
public class MailSenderService {
	
	@Value("${spring.mail.username}")
    private String from;
	
	@Autowired
    private JavaMailSender mailSender;
	
	@Autowired
    private FreeMarkerConfigurer configurer;
	
	private MimeMessage generateMimeMessageHelper(String subject,String text,String[] toMails,File[] files) throws Exception{
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		helper.setFrom(from);
		helper.setTo(toMails);
		helper.setSubject(subject);
		helper.setText(text, true);
		if(files!=null){
			for(File f : files){
				helper.addAttachment(f.getName(), f);
			}
		}
		return mimeMessage;
	}
	
	private String generateTextByTemplate(String templateName,Object params) throws Exception{
		Map<String, Object> model = new HashMap<>();
		model.put("params", params);
		 try{
        	configurer.getConfiguration().setClassLoaderForTemplateLoading(Thread.currentThread().getContextClassLoader(), "template");
        }catch(Exception e){
        	configurer.getConfiguration().setClassLoaderForTemplateLoading(ClassLoader.getSystemClassLoader(), "template");
        }
		Template template = configurer.getConfiguration().getTemplate(templateName);
		return FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
	}

	
	public void sendMessageMail(String subject,String templateName,Object params,String[] toMails) throws Exception{
		String text = generateTextByTemplate(templateName,params);
		MimeMessage mimeMessage = generateMimeMessageHelper(subject,text,toMails,null);
		mailSender.send(mimeMessage);
	}
	
	public void sendMessageMail(String subject,String templateName,Object params,String toMail) throws Exception{
		sendMessageMail(subject,templateName,params,new String[]{toMail});
	}
	
	public void sendMessageMail(String subject,String templateName,Object params,String[] toMails,File file)throws Exception{
		sendMessageMail(subject,templateName,params,toMails,new File[]{file});
	}
	
	public void sendMessageMail(String subject,String templateName,Object params,String toMail,File file)throws Exception{
		sendMessageMail(subject,templateName,params,new String[]{toMail},new File[]{file});
	}
	
	public void sendMessageMail(String subject,String templateName,Object params,String[] toMails,File[] files)throws Exception{
		String text = generateTextByTemplate(templateName,params);
		MimeMessage mimeMessage = generateMimeMessageHelper(subject,text,toMails,files);
		mailSender.send(mimeMessage);
	}
	
	public void sendMessageMail(String subject,String context,String[] toMails,File[] files)throws Exception{
		MimeMessage mimeMessage = generateMimeMessageHelper(subject,context,toMails,files);
		mailSender.send(mimeMessage);
	}
	
	public void sendMessageMail(String subject,String context,String toMails,File[] files)throws Exception{
		MimeMessage mimeMessage = generateMimeMessageHelper(subject,context,new String[]{toMails},files);
		mailSender.send(mimeMessage);
	}
	
	public void sendMessageMail(String subject,String context,String toMails,File file)throws Exception{
		MimeMessage mimeMessage = generateMimeMessageHelper(subject,context,new String[]{toMails},new File[]{file});
		mailSender.send(mimeMessage);
	}
	
	public void sendMessageMail(String subject,String context,String[] toMails,File file)throws Exception{
		MimeMessage mimeMessage = generateMimeMessageHelper(subject,context,toMails,new File[]{file});
		mailSender.send(mimeMessage);
	}
	
	public void sendMessageMail(String subject,String context,String[] toMails)throws Exception{
		MimeMessage mimeMessage = generateMimeMessageHelper(subject,context,toMails,null);
		mailSender.send(mimeMessage);
	}
	
	public void sendMessageMail(String subject,String context,String toMail)throws Exception{
		sendMessageMail(subject,context,new String[]{toMail});
	}
}
